<template>
  <el-dialog title="修改房间" :visible="dialogVisible" width="50%" :before-close="doClose">
    <el-form ref="roomForm" label-position="right" label-width="120px" :model="room" :rules="roomRules">
      <el-form-item prop="name" label="标题">
        <el-input v-model="room.name" />
      </el-form-item>
      <el-form-item prop="bed" label="床">
        <el-select v-model="room.roomBed.id" placeholder="请选择">
          <el-option
            v-for="item in $store.getters.themeHotel.bed"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="breakfast" label="早餐">
        <el-select v-model="room.roomBreakfast.id" placeholder="请选择">
          <el-option
            v-for="item in $store.getters.themeHotel.breakfast"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="wifi" label="WIFI">
        <el-switch
          v-model="room.wifi"
          active-color="#13ce66"
          inactive-color="#D8D8D8"
        />
      </el-form-item>
      <el-form-item prop="price" label="价钱">
        <el-input v-model="room.price" />
      </el-form-item>
      <el-form-item prop="area" label="面积">
        <el-input v-model="room.area" />
      </el-form-item>
      <el-form-item prop="floor" label="楼层">
        <el-input v-model="room.floor" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="doClose">取 消</el-button>
      <el-button type="primary" @click="doUpdate">修 改</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  validStringLength50,
  validNumberLength11,
  validPrice
} from '@/utils/validate'
import { updateRoom } from '@/api/theme-hotel'
export default {
  props: {
    dialogVisible: {
      type: Boolean,
      required: true,
      default: false
    },
    roomm: {
      type: Object,
      required: true,
      default() {
        return {
          id: 0,
          name: '',
          roomBed: {
            id: 1
          },
          roomBreakfast: {
            id: 1
          },
          wifi: false,
          price: 0,
          area: 0,
          floor: '',
          explain: '无',
          hotel: {
            id: 0
          }
        }
      }
    }
  }, data() {
    const validateRoomName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入房间名'))
      } else if (!validStringLength50(value)) {
        callback(new Error('长度1-50'))
      } else {
        callback()
      }
    }
    const validatePrice = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输价格'))
      } else if (!validPrice(value)) {
        callback(new Error('这不是有效的两位小数'))
      } else {
        callback()
      }
    }
    const validateRoomArea = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入面积'))
      } else if (!validNumberLength11(value)) {
        callback(new Error('长度1-11'))
      } else {
        callback()
      }
    }
    const validateRoomFloor = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入热度'))
      } else if (!validStringLength50(value)) {
        callback(new Error('长度0-11'))
      } else {
        callback()
      }
    }
    return {
      room: {
        id: 0,
        name: '',
        roomBed: {
          id: 1
        },
        roomBreakfast: {
          id: 1
        },
        wifi: false,
        price: 0,
        area: 0,
        floor: '',
        explain: '无',
        hotel: {
          id: 0
        }
      },
      roomRules: {
        name: [{ required: true, trigger: 'blur', validator: validateRoomName }],
        price: [{ required: true, trigger: 'blur', validator: validatePrice }],
        area: [{ required: true, trigger: 'blur', validator: validateRoomArea }],
        floor: [{ required: true, trigger: 'blur', validator: validateRoomFloor }]
      }
    }
  },
  watch: {
    roomm() {
      this.room = this.roomm
    }
  },
  created() {
    this.$store.dispatch('themeHotel/setBedAndBreakfast')
  },
  methods: {
    doUpdate() {
      this.$refs.roomForm.validate(valid => {
        if (valid) {
          updateRoom(this.room).then(res => {
            this.$message.success(res.message)
            this.updated(res.data)
          })
        } else {
          this.$message.error('请检查表单')
        }
      })
    },
    updated(room) {
      this.$emit('updated', { room })
      this.doClose()
    },
    doClose() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
